package breadbox.client.logging;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import breadbox.client.files.Chunk;

public class LogChunk extends Chunk {
	
	private static final long serialVersionUID = -4197845438283619862L;
	private boolean compression;
	
	/**
	 * Construct a new LogChunk from another Chunk
	 * @param k Chunk
	 */
	public LogChunk(Chunk k) {
		this(k, true);
	}
	
	/**
	 * Constructs a new LogChunk from another Chunk
	 * @param k Chunk
	 * @param compression boolean
	 */
	public LogChunk (Chunk k, boolean compression) {
		this.data = null;
		this.id = k.getID();
		this.index = k.getIndex();
		this.filename = k.getFilename();
		this.total = k.getTotal();
		this.compression = compression;
	}
	
	/**
	 * Constructs a new LogChunk from a serialized Chunk as a byte[]
	 * @param b byte[] representing serialized Chunk
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public LogChunk(byte[] b) throws IOException, ClassNotFoundException {
		// get chunk from byte[]
		ByteArrayInputStream byte_in = new ByteArrayInputStream(b);
		ObjectInputStream object_in = new ObjectInputStream(byte_in);
		LogChunk k = (LogChunk)object_in.readObject();
		object_in.close();
		byte_in.close();
		
		// get fields from chunk
		if (k != null) {
			this.data = null;
			this.id = k.getID();
			this.index = k.getIndex();
			this.filename = k.getFilename();
			this.total = k.getTotal();
			this.compression = k.getCompression();
		}
	}
	
	/**
	 * Get the compression for the current chunk file
	 * @return boolean compression
	 */
	public boolean getCompression() {
		return this.compression;
	}
}
